﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EShop.BussinessObjects;
using EShop.Common;
using EShop.DataAccess;

namespace OrderingSystem
{
    public partial class ContactEdit : Form
    {
        public bool IsNew = true;
        public User CurrentUser;
        public int UserTypeID;

        public event EventHandler btnSaveClicked;

        public ContactEdit()
        {
            CurrentUser = new User();
            CurrentUser.Contact = new Contact();
            CurrentUser.UserType = new UserType();
            InitializeComponent();
        }

        private void ContactEdit_Load(object sender, EventArgs e)
        {
            switch (UserTypeID)
            {
                case cnstUserType.Customer:
                    cbxUserType.SelectedIndex = 0;
                    cbxSecurityLevel.SelectedIndex = 0;
                    //txtLoginID.Enabled = false;
                    //txtPassword.Enabled = false;
                    break;
                case cnstUserType.DeliveryUser:
                    cbxUserType.SelectedIndex = 1;
                    cbxSecurityLevel.SelectedIndex = 1;
                    break;
                case cnstUserType.LoginUser:
                    cbxUserType.SelectedIndex = 2;
                    cbxSecurityLevel.SelectedIndex = 2;
                    break;
            }
            if (IsNew) SetForm_for_NewUser();
            else SetForm_for_EditUser();
        }

        private void SetForm_for_NewUser()
        {
        }

        private void SetForm_for_EditUser()
        {
            try
            {
                lblContactName.Text = CurrentUser.Contact.FirstName + ' ' + CurrentUser.Contact.LastName;
                txtFirstName.Text = CurrentUser.Contact.FirstName;
                txtLastName.Text = CurrentUser.Contact.LastName;
                txtTel.Text = CurrentUser.Contact.TelNumber;
                txtCell.Text = CurrentUser.Contact.CellNumber;
                txtFax.Text = CurrentUser.Contact.FaxNumber;
                txtEmail.Text = CurrentUser.Contact.EmailAddress;
                //txtAddress.Text = CurrentUser.Contact.Address;
                txtStreet.Text = CurrentUser.Contact.Street;
                txtHomeNumber.Text = CurrentUser.Contact.HomeNumber;
                txtPostalCode.Text = CurrentUser.Contact.PostalCode;
                txtLoginID.Text = CurrentUser.LoginID;
                txtPassword.Text = CurrentUser.Password;
            }
            catch (Exception ex)
            {
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string _errmsg;
            Contact _contact = new Contact();
            User _user = new User();
            _user.UserType = new UserType();
            _user.Contact = new Contact();

            if (IsNew)
            {
                _contact.FirstName = txtFirstName.Text;
                _contact.LastName = txtLastName.Text;
                _contact.TelNumber = txtTel.Text;
                _contact.CellNumber = txtCell.Text;
                _contact.FaxNumber = txtFax.Text;
                _contact.EmailAddress = txtEmail.Text;
                //_contact.Address = txtAddress.Text;
                _contact.Street = txtStreet.Text;
                _contact.HomeNumber = txtHomeNumber.Text;
                _contact.PostalCode = txtPostalCode.Text;
                CDAContact.Insert(_contact, out _errmsg);

                _user.LoginID = txtLoginID.Text;
                _user.Password = txtPassword.Text;
                _user.UserType.ID = Convert.ToInt32(cbxUserType.SelectedIndex + 1);
                _user.SecurityLevel = Convert.ToInt32(cbxSecurityLevel.SelectedIndex + 1);
                _user.Contact.ID = _contact.ID;
                CDAUser.Insert(_user, out _errmsg);
            }
            else
            {
                _contact.ID = CurrentUser.Contact.ID;
                _contact.FirstName = txtFirstName.Text;
                _contact.LastName = txtLastName.Text;
                _contact.TelNumber = txtTel.Text;
                _contact.CellNumber = txtCell.Text;
                _contact.FaxNumber = txtFax.Text;
                _contact.EmailAddress = txtEmail.Text;
                //_contact.Address = txtAddress.Text;
                _contact.Street = txtStreet.Text;
                _contact.HomeNumber = txtHomeNumber.Text;
                _contact.PostalCode = txtPostalCode.Text;
                CDAContact.Update(_contact, out _errmsg);

                _user.ID = CurrentUser.ID;
                _user.LoginID = txtLoginID.Text;
                _user.Password = txtPassword.Text;
                _user.UserType.ID = Convert.ToInt32(cbxUserType.SelectedValue);
                _user.SecurityLevel = Convert.ToInt32(cbxSecurityLevel.SelectedValue);
                _user.Contact.ID = _contact.ID;
                CDAUser.Update(_user, out _errmsg);
            }

            if (btnSaveClicked != null)
                btnSaveClicked(sender, e);
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
